package com.jianbing.controller;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jianbing.dao.MongoDBDao;
import com.jianbing.entity.Goods;
import com.jianbing.entity.Result;
import com.jianbing.exception.ServiceException;
import com.jianbing.util.Constants;
import com.jianbing.util.StringUtils;
import com.jianbing.util.TableUtil;
import com.mongodb.BasicDBObject;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @author cwl
 * @date 2017年5月12日
 *
 */
@Controller
@RequestMapping(value = "/goods")
public class GoodsController {
	Logger logger = Logger.getLogger(GoodsController.class);

	
	
     /*添加产品*/
	@SuppressWarnings("static-access")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Result addGoods(HttpServletRequest request, HttpServletResponse response, @ModelAttribute Goods goods) {
		Result result = new Result();
		if (StringUtils.isEmpty(goods.getGoodsSn()) || StringUtils.isEmpty(goods.getGoodsName())
				|| goods.getState() == null || StringUtils.isEmpty(goods.getGoodsType())) {
			result.setCode(101);
			result.setMsg("参数错误");
			return result;
		}
		try {
			MongoDBDao dbDao = new MongoDBDao();
			BasicDBObject queryObject = new BasicDBObject();
			queryObject.append("goodsSn", goods.getGoodsSn());
			Goods existGoods = dbDao.findOne(TableUtil.goods, Goods.class, queryObject);
			if (existGoods != null) {
				result.setCode(102);
				result.setMsg("商品编号重复");
				return result;
			}
			dbDao.insert(TableUtil.goods, goods);
		} catch (ServiceException e) {
			result.setCode(-1);
			result.setMsg("系统异常");
		}
		return result;
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Result updateGoods(HttpServletRequest request, HttpServletResponse response, @ModelAttribute Goods goods) {
		Result result = new Result();
		try {
			if (StringUtils.isEmpty(goods.getGoodsSn()) || StringUtils.isEmpty(goods.getGoodsName())
					|| goods.getState() == null || StringUtils.isEmpty(goods.getGoodsType())) {
				result.setCode(101);
				result.setMsg("参数错误");
				return result;
			}
			BasicDBObject queryObject = new BasicDBObject();
			queryObject.append("goodsSn", goods.getGoodsSn());
			long count = MongoDBDao.update(TableUtil.goods, queryObject, goods);
			if (count <= 0) {
				result.setCode(102);
				result.setMsg("更新失败");
				return result;
			}
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			result.setCode(-1);
			result.setMsg("系统异常");
		}
		return result;
	}

	@RequestMapping(value = "/query")
	@ResponseBody
	public Result queryGoods(HttpServletRequest request, HttpServletResponse response, @ModelAttribute Goods goods) {
		Result result = new Result();
		ModelMap model = new ModelMap();
		BasicDBObject queryObject = new BasicDBObject();
		if (!StringUtils.isEmpty(goods.getGoodsSn())) {
			queryObject.append("goodsSn", goods.getGoodsSn());
		}

		if (!StringUtils.isEmpty(goods.getGoodsName())) {
			queryObject.append("goodsName", goods.getGoodsName());
		}

		if (goods.getState() != null) {
			queryObject.append("state", goods.getState());
		}

		if (StringUtils.isNotEmpty(goods.getGoodsType())) {
			queryObject.append("goodsType", goods.getGoodsType());
		}

		try {
			BasicDBObject sortObject = new BasicDBObject();
			sortObject.append("goodsSn", -1);
			List<Goods> list = MongoDBDao.queryForList(TableUtil.goods, Goods.class, queryObject, null, sortObject, null, null);
			model.addAttribute("goodsList", list);
			result.setData(model);
		} catch (Exception e) {
			result.setCode(-1);
			result.setMsg("系統异常");
		}
		return result;
	}
}
